﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiTest.Dto.Basic;
using WebApiTest.Dto.Utils;
using WebApiTest.Models;

namespace WebApiTest.Service.Office

{
    public class CollectService
    {
        officeContext db = new officeContext();

        public TableRspDTO list4Table(TableReqDTO tableReqDTO)
        {
            long count = db.Collect
                .Join(db.OfficeObject, c => c.ObjectId, o => o.ObjectId, (c, o) => new { objectName = o.ObjectName, typeId = o.TypeId, personId = c.PersonId })
                .Join(db.Person, c => c.personId, p => p.PersonId, (c, p) => new { objectName = c.objectName, typeId = c.typeId, name = p.Name })
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("name")) || u.name.Contains(tableReqDTO.parseQueryParam("name")))
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("objectName")) || u.objectName.Contains(tableReqDTO.parseQueryParam("objectName")))
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("typeId")) || u.typeId.ToString().Equals(tableReqDTO.parseQueryParam("typeId")))
                .LongCount();
            List<JObject> res = db.Collect
                .Join(db.Person, c => c.PersonId, p => p.PersonId, 
                (c, p) => new { collectId = c.CollectId, personId = c.PersonId, objectId = c.ObjectId, collectTime = c.CollectTime, collectNum = c.CollectNum, name = p.Name })
                .Join(db.OfficeObject, c => c.objectId, 
                o => o.ObjectId, (c, o) => new { key = c.collectId, collectId = c.collectId, personId = c.personId, objectId = c.objectId, collectTime = c.collectTime, collectNum = c.collectNum, name = c.name, typeId = o.TypeId, objectName = o.ObjectName })
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("name")) || u.name.Contains(tableReqDTO.parseQueryParam("name")))
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("objectName")) || u.objectName.Contains(tableReqDTO.parseQueryParam("objectName")))
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("typeId")) || u.typeId.ToString().Equals(tableReqDTO.parseQueryParam("typeId")))
                .OrderByDescending(u => u.collectId)
                .Skip(tableReqDTO.getStart())
                .Take(tableReqDTO.pageSize)
                .ToList()
                .ConvertAll(r => JObject.FromObject(r));
            PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.pageNo,
                tableReqDTO.pageSize,
                count,
                res
                );
            return new TableRspDTO(pagingDTO);
        }

        public int add(Collect collect)
        {
            collect.CollectTime = DateTime.Now;
            db.Collect.Add(collect);
            OfficeObject officeObject = db.OfficeObject.Find(collect.ObjectId);
            officeObject.Number -= collect.CollectNum;
            officeObject.GmtUpdate = DateTime.Now;
            db.SaveChanges();
            return 1;
        }

        public int update(Collect collect)
        {
            Collect c = db.Collect.Find(collect.CollectId);
            OfficeObject old = db.OfficeObject.Find(c.ObjectId);
            old.Number += c.CollectNum;
            OfficeObject new1 = db.OfficeObject.Find(collect.ObjectId);
            new1.Number -= collect.CollectNum;
            new1.GmtUpdate = DateTime.Now;
            c.ObjectId = collect.ObjectId;
            c.PersonId = collect.PersonId;
            c.CollectNum = collect.CollectNum;
            c.CollectTime = DateTime.Now;
            db.SaveChanges();
            return 1;
        }
        public int remove(string ids)
        {
            List<Collect> list = new List<Collect>();
            string[] idList = ids.Split(",");
            foreach (string id in idList)
            {
                Collect c = db.Collect.Find(long.Parse(id));
                list.Add(c);
                OfficeObject old = db.OfficeObject.Find(c.ObjectId);
                old.Number += c.CollectNum;
            }
            db.Collect.RemoveRange(list);
            //更新到数据库
            db.SaveChanges();
            return list.Count();
        }
    }
}
